angular.module('starter.controllers', [])
    .controller('MainCtrl', function ($rootScope, $scope, $window, $location,$cookieStore,Static,Nav,OpenId) {

        $scope.loadUrl = Static.host + '/?r=site/getImage&url='


        $rootScope.window = $window;
        $rootScope.jqueryInit = function () {
            $(function () {
                Public.init();
                var mySwiper1 = new Swiper('.scroll_wrap',{
                    freeMode : true,
                    slidesPerView : 'auto'
                });
            })
            $rootScope.location = $location;
            $scope.url = $location.$$path;
        }
        OpenId.then(function(res){
            console.log(res);
        });









    })
    .controller('HomeCtrl', function ($rootScope, $scope,Nav, getData) {
        Nav.then(function(res){
            if(res.rows.length)
                $rootScope.navs = res.rows;

                var req = {playlistids:res.rows[0].playlistids};
               $scope.getData = new getData(req);

        })
        $rootScope.jqueryInit();




    })
    .controller('ViewCtrl', function ($rootScope, $scope, $http, $stateParams,Static,$window,Nav) {

        $window.scroll(0,0);
        Nav.then(function(res){
            if(res.rows.length)
                $rootScope.navs = res.rows;
        })

        $rootScope.jqueryInit();
        $(function () {

            Detail.init();
        })
        $scope.data = {};
        var id = parseInt($stateParams.id);
        var url =Static.host + '/?r=TVPlaylist/video&data={"playlistid":' + id + '}&callback=JSON_CALLBACK';

        $http.jsonp(url).success(function (res) {
            $scope.list = res.rows;
            $scope.video = res.playlist;


        })
        $scope.showVideo = function (item) {
            showVideo(item, $scope);
         }
   })
    .controller('ListCtrl',function($rootScope, $scope, $stateParams,getData,Nav){

        $rootScope.jqueryInit();
        Nav.then(function(res){
            if(res.rows.length)
                $rootScope.navs = res.rows;
        })

        var id = $stateParams.id || '';
        var req = {playlistids:id};
        $scope.getData = new getData(req,$rootScope);



    })
    .controller('SearchCtrl',function($rootScope, $scope,$cookieStore,getData,Static,$http){

        $rootScope.jqueryInit();
        $scope.showHis = false;
        $scope.his = $cookieStore.get('kwds');

        var url =Static.host + '/?r=site/getHotKwds&callback=JSON_CALLBACK';
        $http.jsonp(url).success(function (res) {
            $scope.hotkwds = res.rows;
         })
        $scope.ckshowHis = function(){

            $scope.showHis = ! $scope.showHis;

        }
        $scope.searchKwd = function(kwd){
            $scope.kwd = kwd;

            if(!kwd) return;
            location.href = '#/search/'+kwd
         }



    })
    .controller('SearchKwdCtrl',function($rootScope, $scope,$cookieStore,$stateParams,getData){

        $rootScope.jqueryInit();
        var kwd = $stateParams.kwd || '';
        var req = {kwd:kwd};

        var cookieKwd = $cookieStore.get('kwds') || [];
        cookieKwd.push(kwd);
        cookieKwd= cookieKwd.unique();
        $cookieStore.put('kwds',cookieKwd);

        $scope.getData = new getData(req);
        console.log($scope.getData);



    })

function showVideo(item, $scope) {
    //window.plugins.screen.lockOrientation('landscape');
    var videoUrl = item.url;
    var options = {
       //isStreaming: true,
       // bgColor: "#FFFFFF",
        bgImage: item.pic,
        bgImageScale: "fit"
//        successCallback: function() {
//            console.log("Player closed without error.");
//        },
//        errorCallback: function(obj) {
//            console.log("Error! " + obj.message);
//        }
    };
    window.plugins.streamingMedia.playVideo(videoUrl, options);
    //console.log('player1111');
}




